﻿var typename = ["統率指揮官型","武統指揮官型","武力武官型","武知武官型","知力文官型","知統文官型","武統万能型","武知万能型","知統万能型","万能型"];
var tokuginame = ["先制攻撃","一喝","偽令","連突","兵糧庫急襲","煉獄の計","ランダム"];
var bonusname = ["訓練度最低値固定・義勇兵","猛訓練","米売買","罠設置","米消費","訓練時武力ＵＰ","巡察訓練","精神統一"];

// 職変更部分はコピーです。
function jobchangeBukan(str, inte, lea, jfs, jfi, jfl ){
 upstr = str; upinte = inte; uplea = lea; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 if(upjfs >= 80 && upjfl >= 65){
  while(upjfl >= 65){
   upstr++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
 }else{
  while(!(upjfs >= 80)){
  upstr++;
  sum = upstr + upinte + uplea;temp = sum / 150;
  upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
 }
 uptemp = upstr;
 jobchange = "武+" + (upstr - str)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 upstr = str; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 while(!(upjfs >= 65 && upjfi >= 65)){
  if(upstr > upinte){
   upinte++;
  } else{
   upstr++;
  }
  sum = upstr + upinte + uplea;temp = sum / 150;
  upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
 }
 if(upstr < uptemp && upstr != str && upinte != inte){
  jobchange += " 武+"+ (upstr - str) + "知+"+ (upinte - inte) +"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }else if(upstr < uptemp && upinte != inte){
  jobchange += " 知+"+ (upinte - inte) +"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }else if(upstr < uptemp && upstr != str){
  jobchange = "武+" + (upstr - str)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }
 return jobchange + "";
}

function jobchangeBunkan(str, inte, lea, jfs, jfi, jfl ){
 upstr = str; upinte = inte; uplea = lea; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 if(upjfs >= 65 && upjfi >= 80){
  while(upjfs >= 65){
   upinte++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
 }else if(upjfi >= 65 && upjfl >= 80){
  while(upjfl >= 80){
   upinte++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
 }else{
  while(!(upjfi >= 80)){
   upinte++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
 }
 uptemp = upinte;
 jobchange = "知+"+ (upinte - inte)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 upinte = inte; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 while(!(upjfi >= 65 && upjfl >= 65)){
  if(upinte > uplea){
   uplea++;
  } else{
   upinte++;
  }
  sum = upstr + upinte + uplea;temp = sum / 150;upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
 }
 if(upinte < uptemp && uplea != lea && upinte != inte){
  jobchange += " 知+"+ (upinte - inte) + "統+"+ (uplea - lea)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }else if(upinte < uptemp && uplea != lea){
  jobchange += " 統+"+ (uplea - lea) +"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }else if(upinte < uptemp && upinte != inte){
  jobchange = "知+"+ (upinte - inte)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }
 return jobchange + "";
}

function jobchangeShikikan(str, inte, lea, jfs, jfi, jfl ){
 upstr = str; upinte = inte; uplea = lea; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 while(!(upjfl >= 80)){
  uplea++;
  sum = upstr + upinte + uplea;temp = sum / 150;
  upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
 }
 uptemp = uplea;
 jobchange = "統+"+ (uplea - lea) +"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 uplea = lea; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 while(!(upjfs >= 65 && upjfl >= 65)){
  if(upstr > uplea){
   uplea++;
  } else{
   upstr++;
  }
  sum = upstr + upinte + uplea;temp = sum / 150;
  upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
 }
 if(uplea < uptemp && uplea != lea && upstr != str){
  jobchange += " 武+"+ (upstr - str) + "統+"+ (uplea - lea)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }else if(uplea < uptemp && upstr != str){
  jobchange += " 武+"+ (upstr - str) +"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }else if(uplea < uptemp && uplea != lea){
  jobchange = "統+"+ (uplea - lea) +"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
 }
 return jobchange + "";
}

function jobchangeBanno(str, inte, lea, jfs, jfi, jfl, key ){
 upstr = str; upinte = inte; uplea = lea; upjfs = jfs; upjfi = jfi; upjfl = jfl;
 if(key == "bukan"){
  while(upjfs >= 80 || (upjfs >= 65 && upjfi >= 65)){
   uplea++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
  if(jfs == jfi && jfs >= 65 && jfi >= 65){
   if(uplea - lea > uplea / temp){
    jobchange = "統+"+ (uplea - lea)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
   } else{
    jobchange = "(知+統)+"+ (uplea - lea)+"["+upstr+","+(upinte + uplea - lea)+","+lea+"]-["+upstr+","+inte+","+uplea+"]";
   }
  } else if((jfs > jfi && jfs >= 65 && jfi >= 65) || (jfs >= 65 && jfi >= 80)){
   jobchange = "統+"+ (uplea - lea)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
  } else{
   jobchange = "(知+統)+"+ (uplea - lea)+"["+upstr+","+(upinte + uplea - lea)+","+lea+"]-["+upstr+","+inte+","+uplea+"]";
  }
  return jobchange + "";
 }else if(key == "bunkan"){
  while(upjfi >= 80 || (upjfi >= 65 && upjfl >= 65)){
   upstr++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
  if(jfi == jfl && jfi >= 65 && jfl >= 65){
   if(upstr - str > upstr / temp){
    jobchange = "武+"+ (upstr - str)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
   } else{
    jobchange = "(武+統)+"+ (upstr - str)+"["+upstr+","+upinte+","+lea+"]-["+str+","+upinte+","+(uplea + upstr - str)+"]";
   }
  } else if(jfi > jfl && jfi >= 65 && jfl >= 65){
   jobchange = "武+"+ (upstr - str)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
  } else{
   jobchange = "(武+統)+"+ (upstr - str)+"["+upstr+","+upinte+","+lea+"]-["+str+","+upinte+","+(uplea + upstr - str)+"]";
  }
  return jobchange + "";
 }else {
  while(upjfl >= 80 || (upjfs >= 65 && upjfl >= 65)){
   upinte++;
   sum = upstr + upinte + uplea;temp = sum / 150;
   upjfs = upstr / temp;upjfi = upinte / temp;upjfl = uplea / temp;
  }
  if(jfl == jfs && jfs >= 65 && jfl >= 65){
   if(upinte - inte > upinte / temp){
    jobchange = "知+"+ (upinte - inte)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
   } else{
    jobchange = "(武+知)+"+ (upinte - inte)+"["+(upstr+upinte - inte)+","+inte+","+uplea+"]-["+str+","+upinte+","+uplea+"]";
   }
  } else if((jfl > jfs && jfs >= 65 && jfl >= 65) || (jfs >= 80 && jfl >= 65)){
   jobchange = "知+"+ (upinte - inte)+"["+upstr+","+upinte+","+uplea+"]("+parseInt(upjfs*2)+","+parseInt(upjfi*2)+","+parseInt(upjfl*2)+")";
  } else{
   jobchange = "(武+知)+"+ (upinte - inte)+"["+(upstr+upinte - inte)+","+inte+","+uplea+"]-["+str+","+upinte+","+uplea+"]";
  }
  return jobchange + "";
 }
}

function katas(){
var img=document.getElementsByTagName("img")[0];
var tr = img.parentNode.parentNode;
var tdc = tr.childNodes[2].className;
var table = tr.parentNode.parentNode;
var rspan = img.parentNode.rowSpan;

var bur = eval(tr.childNodes[2].innerHTML);
var chi = eval(tr.childNodes[4].innerHTML);
var tou = eval(tr.childNodes[6].innerHTML);

var sou = bur+chi+tou;

var jfs = parseInt(bur*300/sou);
var jfi = parseInt(chi*300/sou);
var jfl = parseInt(tou*300/sou);
var type = 0;
var tokugi = "";
var bonus = "";
var keiryaku1 = 300 - jfi;
var keiryaku1per = Math.floor(10*100*30/keiryaku1)/10;
if (keiryaku1 < 30) {
keiryaku1per = 100;
}
var keiryaku2 = parseInt(keiryaku1/2);
var keiryaku2per = Math.floor(10*100*30/keiryaku2)/10;
if (keiryaku2 < 30) {
keiryaku2per = 100;
}
if (jfl>159){
type=0;
tokugi += tokuginame[0];
bonus += bonusname[0];
}else if(jfl>129&&jfs>129){
type=1;
tokugi += tokuginame[3];
bonus += bonusname[0];
}else if(jfs>159){
type=2;
tokugi += tokuginame[1];
bonus += bonusname[1];
}else if(jfi>129&&jfs>129){
type=3;
tokugi += tokuginame[4];
bonus += bonusname[1];
}else if(jfi>159){
type=4;
tokugi += tokuginame[2];
bonus += bonusname[2];
}else if(jfi>129&&jfl>129){
type=5;
tokugi += tokuginame[5];
bonus += bonusname[2];
}else if(jfl+jfs>259){
type=6;
tokugi += tokuginame[3];
bonus += bonusname[6];
}else if(jfi+jfs>259){
type=7;
tokugi += tokuginame[4];
bonus += bonusname[6];
}else if(jfi+jfl>259){
type=8;
tokugi += tokuginame[5];
bonus += bonusname[6];
}else{
type=9;
tokugi += tokuginame[6];
bonus += bonusname[6];
}

if (jfl>159) {
bonus += "・"+bonusname[4]+"（大）";
} else if (jfl>129){
bonus += "・"+bonusname[4]+"（小）";
}
if (jfi>129){
if (jfs>129){
bonus += "・"+bonusname[5];
}
bonus += "・"+bonusname[3];
}
if (jfi>89 && jfl>89 && jfs>89){
bonus += "・"+bonusname[7]+"（確率大）";
}else if (jfi>59 && jfl>59 && jfs>59){
bonus += "・"+bonusname[7]+"（確率小）";
}
jfs = jfs/2;
jfi = jfi/2;
jfl = jfl/2;
var jct = "";
var jcb = "";
var jcc = "";
var jca = "";
if (type != 0 && type != 1) {
jct = jobchangeShikikan(bur,chi,tou,jfs,jfi,jfl);
} else {
jca = jobchangeBanno(bur,chi,tou,jfs,jfi,jfl,"shikikan");
}
if (type != 2 && type != 3) {
jcb = jobchangeBukan(bur,chi,tou,jfs,jfi,jfl);
} else {
jca = jobchangeBanno(bur,chi,tou,jfs,jfi,jfl,"bukan");
}
if (type != 4 && type != 5) {
jcc = jobchangeBunkan(bur,chi,tou,jfs,jfi,jfl);
} else {
jca = jobchangeBanno(bur,chi,tou,jfs,jfi,jfl,"bunkan");
}

jfs = jfs*2;
jfi = jfi*2;
jfl = jfl*2;
var tr1=document.createElement("tr");
tr1.className = tr.className;
var td1=document.createElement("td");
var td2=document.createElement("td");
td2.className = tdc;
if (rspan == 4) {
td1.colSpan = "2";
table.getElementsByTagName("tr")[9].firstChild.rowSpan = 4;
} else {
}
td2.colSpan = "6";
tr1.appendChild(td1);
tr1.appendChild(td2);
var tr2=tr1.cloneNode(true);
var tr3=tr1.cloneNode(true);
var tr4=tr1.cloneNode(true);
tr1.firstChild.innerHTML = "型";
tr1.lastChild.innerHTML = "["+bur+","+chi+","+tou+"]("+jfs+","+jfi+","+jfl+")："+typename[type];
tr2.firstChild.innerHTML = "特技";
tr2.lastChild.innerHTML = tokugi;
tr3.firstChild.innerHTML = "ボーナス";
tr3.lastChild.innerHTML = bonus;
tr4.firstChild.innerHTML = "計略係数";
tr4.lastChild.innerHTML = "30/"+keiryaku1+"("+keiryaku1per+"％) "+"30/"+keiryaku2 +"("+keiryaku2per+"％)"+"[影武者配置]";

tr.parentNode.appendChild(tr1);
tr.parentNode.appendChild(tr2);
tr.parentNode.appendChild(tr3);
tr.parentNode.appendChild(tr4);

if (jcb) {
var tr5=tr1.cloneNode(true);
tr5.firstChild.innerHTML = " ⇒武官型";
tr5.lastChild.innerHTML = jcb;
tr.parentNode.appendChild(tr5);
}
if (jcc) {
var tr5=tr1.cloneNode(true);
tr5.firstChild.innerHTML = " ⇒文官型";
tr5.lastChild.innerHTML = jcc;
tr.parentNode.appendChild(tr5);
}
if (jct) {
var tr5=tr1.cloneNode(true);
tr5.firstChild.innerHTML = " ⇒指揮官型";
tr5.lastChild.innerHTML = jct;
tr.parentNode.appendChild(tr5);
}
if (jca) {
var tr5=tr1.cloneNode(true);
tr5.firstChild.innerHTML = " ⇒万能型";
tr5.lastChild.innerHTML = jca;
tr.parentNode.appendChild(tr5);
}
}
katas();